package cqrtplm.vo.query;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
public class AdvancedQueryConditionVO {
    private String tableAlias;
    private String field;
    private String operator;
    private Object value;
    private String logic;

    private boolean validated = false;
    private String validationError;

    private List<SortCondition> sortConditions;
    private Integer pageNum;
    private Integer pageSize;

    public AdvancedQueryConditionVO() {}

    public AdvancedQueryConditionVO(String field, String operator, Object value) {
        this.field = field;
        this.operator = operator;
        this.value = value;
        this.logic = "AND";
    }

    public boolean validate() {
        if (field == null || operator == null) {
            validationError = "字段名和操作符不能为空";
            return false;
        }

        if (!AdvancedQueryValidator.isValidFieldName(field)) {
            validationError = "字段名 '" + field + "' 格式不合法";
            return false;
        }

        if (!AdvancedQueryValidator.isValidTableAlias(tableAlias)) {
            validationError = "表别名 '"+ tableAlias + "' 格式不合法";
            return false;
        }

        if (!AdvancedQueryValidator.isValidOperator(operator)) {
            validationError = "操作符 '" + operator + "' 不被允许";
            return false;
        }

        if (!validateValueByOperator()) {
            validationError = "值验证失败";
            return false;
        }

        if (sortConditions != null) {
            for (SortCondition sort : sortConditions) {
                if (!AdvancedQueryValidator.isValidFieldName(sort.getField()) ||
                        !AdvancedQueryValidator.isValidSortDirection(sort.getDirection())) {
                    validationError = "排序条件验证失败";
                    return false;
                }
            }
        }

        validated = true;
        return true;
    }

    private boolean validateValueByOperator() {
        if (operator.equals("IS NULL") || operator.equals("IS NOT NULL")) {
            return value == null;
        }

        if (value == null) {
            validationError = "操作符 " + operator + " 需要非空值";
            return false;
        }

        if ((operator.equals("LIKE") || operator.equals("NOT LIKE")) && value instanceof String) {
            return AdvancedQueryValidator.isValidLikeValue((String) value);
        }

        if (value instanceof String) {
            return !AdvancedQueryValidator.containsSqlInjection((String) value);
        }

        if (value instanceof List) {
            List<?> list = (List<?>) value;
            for (Object item : list) {
                if (item instanceof String && AdvancedQueryValidator.containsSqlInjection((String) item)) {
                    return false;
                }
            }
        }

        return true;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SortCondition {
        private String field;
        private String direction;
    }
}
